package com.nw.news.ui;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.nw.news.R;
import com.nw.news.bean.Notice;
import com.yuan.base.framework.BaseFrameworkFragment;
import com.yuan.base.framework.BaseViewHolderAdapter;
import com.yuan.base.utils.DateUtil;
import com.yuan.base.widget.AutoLoadList;

import java.util.ArrayList;
import java.util.List;

/**
 * 直播
 */
public class MainVideoFragment extends BaseFrameworkFragment {

	// --------------------------控件--------------------------
	private AutoLoadList auto_load_list;
	private ListView data_list;// 下拉刷新
	// --------------------------变量--------------------------
	private List<Notice> dataList;
	private NoticeAdapter noticeAdapter;
	private int index = 0;

	@Override
	protected int getLayoutId() {
		return R.layout.fragment_main_video;
	}

	@Override
	protected void bindViews() {
		auto_load_list = findViewById(R.id.auto_load_list);
		data_list = auto_load_list.getData_list();
		auto_load_list.setNeedShowFooter(false);
		auto_load_list.setOnRefreshingListener(new AutoLoadList.OnRefreshingListener() {
			@Override
			public void onRefresh() {
				getListData(AutoLoadList.OPT_REFREASH);
			}

			@Override
			public void onLoadMore() {
				getListData(AutoLoadList.OPT_LOADMORE);
			}
		});

		data_list.setOnItemClickListener((parent, view, position, id) -> {
//			Intent intent = new Intent(context, VideoDetailActivity.class);
//			intent.putExtra("video", (Live) parent.getItemAtPosition(position));
//			startActivity(intent);
			Intent intent = new Intent(getActivity(), H5Activity.class);
			intent.putExtra("notice", (Notice) parent.getItemAtPosition(position));
			startActivity(intent);
		});
	}

	@Override
	protected void initUI() {
		auto_load_list.startRefreshing();
	}

	private void getListData(final int opt) {
		dataList = new ArrayList<>();
		addData();
		addData();
		addData();


//		Notice notice4 = new Notice();
//		notice4.setTitle("摄影家14年行走72座城堡 愿做“烽火台”记录长城文化");
//		notice4.setPublisher("中国新闻网");
//		notice4.setPublishDate(DateUtil.stringToDate("2019-10-29 11:50:41"));
//		notice4.setContent("https://m.chinanews.com/wap/detail/sp/sh/shipin/cns/2019/10-30/news8993093.shtml");
//		notice4.setSchoolId(R.drawable.temp14);
//		noticeList.add(notice4);


		init(true);
		auto_load_list.completeRefresh();

//		if (opt == AutoLoadList.OPT_INIT || opt == AutoLoadList.OPT_REFREASH) {
//			index = 0;
//		}
//		if (opt == AutoLoadList.OPT_LOADMORE) {
//			index++;
//		}
//
//		HashMap<String, Object> params = new HashMap<>();
//		params.put("page", index);
//		params.put("size", auto_load_list.getRowCount());
//		BaseHttpRequest.get(Urls.getUrl(Urls.getLiveVideo), params, new BaseHttpCallBack<JSONObject>() {
//
//			@Override
//			public void onSuccess(JSONObject result) {
//				if (result != null && result.containsKey("content")) {
//					List<Live> resultList = JSON.parseArray(result.getString("content"), Live.class);
//					switch (opt) {
//						case AutoLoadList.OPT_INIT:
//						case AutoLoadList.OPT_REFREASH:
//							dataList = resultList;
//							init(true);
//							break;
//						case AutoLoadList.OPT_LOADMORE:
//							dataList.addAll(resultList);
//							init(false);
//							break;
//					}
//				}
//			}
//
//			@Override
//			public void onPost() {
//				auto_load_list.completeRefresh();
//			}
//		});

	}

	private void addData(){
		List<Notice> noticeList = new ArrayList<>();
		Notice notice = new Notice();
		notice.setTitle("华为真的太难了！手机对战苹果，芯片还要对战高通英特尔！");
		notice.setPublisher("腾讯新闻");
		notice.setPublishDate(DateUtil.stringToDate("2019-08-30 22:30:09"));
		notice.setContent("https://xw.qq.com/a/video/u3015erh9yp");
		notice.setSchoolId(R.drawable.temp10);
		noticeList.add(notice);

		Notice notice2 = new Notice();
		notice2.setTitle("任正非谈5G风险：5G就是高速路 你车开得不好能怪路危险吗");
		notice2.setPublisher("腾讯新闻");
		notice2.setPublishDate(DateUtil.stringToDate("2019-08-30 01:37:27"));
		notice2.setContent("https://xw.qq.com/a/video/g3015mdtob9");
		notice2.setSchoolId(R.drawable.temp11);
		noticeList.add(notice2);

		Notice notice3 = new Notice();
		notice3.setTitle("诺贝尔奖得主：人工智能是金融业重大创新 可以减少欺诈");
		notice3.setPublisher("腾讯新闻");
		notice3.setPublishDate(DateUtil.stringToDate("2019-10-29 21:50:41"));
		notice3.setContent("https://xw.qq.com/a/video/g3015vr8uk0");
		notice3.setSchoolId(R.drawable.temp12);
		noticeList.add(notice3);
		dataList.addAll(noticeList);
	}

	@Override
	public void onHiddenChanged(boolean hidden) {
		super.onHiddenChanged(hidden);
	}

	@Override
	public void onResume() {
		super.onResume();
	}


	@Override
	public void onStart() {
		super.onStart();
	}

	private void init(boolean needNew) {
		if (needNew) {
			noticeAdapter = new NoticeAdapter(getActivity());
			noticeAdapter.setData(dataList);
			data_list.setAdapter(noticeAdapter);
		} else {
			noticeAdapter.setData(dataList);
			noticeAdapter.notifyDataSetChanged();
		}
	}

	private class NoticeAdapter extends BaseViewHolderAdapter<Notice, Holder> {

		private NoticeAdapter(Context context) {
			super(context);
		}

		@Override
		protected void bindViews(View convertView, Holder holder, Notice item) {
			holder.tv_notice_title = convertView.findViewById(R.id.tv_notice_title);
			holder.tv_notice_publisher = convertView.findViewById(R.id.tv_notice_publisher);
			holder.tv_notice_publish_time = convertView.findViewById(R.id.tv_notice_publish_time);
			holder.iv_notice_first_img = convertView.findViewById(R.id.iv_notice_first_img);
		}

		@Override
		protected View getConvertView(Notice item, LayoutInflater inflater, int position, ViewGroup parent) {
			return inflater.inflate(R.layout.item_live_list, parent, false);
		}

		@Override
		protected Holder getHolder() {
			return new Holder();
		}

		@Override
		protected void refreshView(int position, Notice item, View convertView, Holder holder) {
			holder.tv_notice_title.setText(item.getTitle());
			holder.tv_notice_publisher.setText(item.getPublisher());
			holder.tv_notice_publish_time.setText(DateUtil.dateToString(item.getPublishDate()).substring(5, 16));
			holder.iv_notice_first_img.setImageResource(item.getSchoolId());
//			if (item.getPicsList() != null && item.getPicsList().length > 0) {
//				BaseImageLoad.load(item.getPicsList()[0], holder.iv_notice_first_img);
//			} else {
//				holder.iv_notice_first_img.setImageResource(R.drawable.base_ic_loading);
//			}
		}
	}

	private class Holder {
		TextView tv_notice_title;
		TextView tv_notice_publisher;
		TextView tv_notice_publish_time;
		ImageView iv_notice_first_img;
	}
}
